package cn.ondraw.ragflow.service.impl;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.request.ChatCompletionRequest;
import cn.ondraw.ragflow.pojo.response.ChatCompletionResponse;
import cn.ondraw.ragflow.service.RagFlowChatCompletionService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class RagFlowChatCompletionServiceImpl implements RagFlowChatCompletionService {

    private final PythonApiCaller pythonApiCaller;

    @Override
    public ChatCompletionResponse createChatCompletion(String chatId, ChatCompletionRequest request) {
        String url = "/chats_openai/" + chatId + "/chat/completions";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(url, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, ChatCompletionResponse.class, true);
    }

    @Override
    public ChatCompletionResponse createAgentCompletion(String agentId, ChatCompletionRequest request) {
        String url = "/agents_openai/" + agentId + "/chat/completions";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(url, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, ChatCompletionResponse.class, true);
    }
}
